package com.xiaoshuidi.cloud.module.member.service.card;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.RecognizeBankCardRequest;
import com.aliyun.ocr_api20210707.models.RecognizeBankCardResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.member.controller.app.card.vo.*;
import com.xiaoshuidi.cloud.module.member.convert.card.MemberUserBankCardConvert;
import com.xiaoshuidi.cloud.module.member.pojo.card.MemberUserBankCardEntity;
import com.xiaoshuidi.cloud.module.member.mapper.card.MemberUserBankCardMapper;
import com.xiaoshuidi.cloud.module.member.enums.card.MemberUserBankCardStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author Leo.Li
 * @version generator
 * @description 租客银行卡service层
 * @date 2023-05-19 17:20:47
 */
@Service
public class MemberUserBankCardService {

    Logger logger = LoggerFactory.getLogger(MemberUserBankCardService.class);
    @Autowired
    MemberUserBankCardMapper memberUserBankCardMapper;

    @Autowired
    Client client;

    /**
     * 列表查询
     */
    public List<MemberUserBankCardDto> listMemberBankCard(MemberUserBankCardParamDto paramDto) throws ServiceException {
        if (Objects.isNull(paramDto)) {
            return Collections.emptyList();
        }
        List<MemberUserBankCardDto> dtoList = memberUserBankCardMapper.list(paramDto);
        if (Objects.isNull(dtoList) || dtoList.isEmpty()) {
            return Collections.emptyList();
        }
        return dtoList;
    }

    public void bindMemberBankCard(MemberUserBankCardForm form) {
        MemberUserBankCardEntity entity = MemberUserBankCardConvert.INSTANCE.convert(form);
        entity.setAccountId(SecurityFrameworkUtils.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        entity.setStatus(MemberUserBankCardStatusEnum.YES.getStatus());
        entity.setBindCardJson(JSONObject.toJSONString(form));
        memberUserBankCardMapper.insert(entity);
    }

    public void disableMemberBankCard(Long id) {
        MemberUserBankCardEntity entity = memberUserBankCardMapper.selectById(id);
        entity.setUpdateTime(LocalDateTime.now());
        entity.setStatus(MemberUserBankCardStatusEnum.NO.getStatus());
        memberUserBankCardMapper.deleteById(entity);
    }

    public List<MemberUserBankCardVo> listMemberBankCardById(Long tenantUserId) {
        return memberUserBankCardMapper.selectMemberBankCardByTenantId(tenantUserId);
    }

    public OcrBankCardVo orcBankCard(MultipartFile file) throws Exception {
        RecognizeBankCardRequest recognizeBankCardRequest = new RecognizeBankCardRequest().setBody(file.getInputStream());
        RuntimeOptions runtime = new RuntimeOptions();
        RecognizeBankCardResponse response = client.recognizeBankCardWithOptions(recognizeBankCardRequest, runtime);
        logger.info("{}", response.getBody().getData());
        String data = response.getBody().getData();
        JSONObject temp = JSONObject.parseObject(data).getJSONObject("data");
        return temp.toJavaObject(OcrBankCardVo.class);
    }

    public void updateUserId(Long oldUserId, Long userId) {
        memberUserBankCardMapper.updateUserId(oldUserId, userId);
    }
}

